package com.theme.passport.controller.app;

import com.theme.common.base.JSONHandler;
import com.theme.common.frame.exception.ErrorCode;
import com.theme.common.mvc.validator.ValidatorUtils;
import com.theme.common.mvc.view.R;
import com.theme.passport.annotation.Login;
import com.theme.passport.controller.app.utils.AppContextUtil;
import com.theme.passport.entity.UserEntity;
import com.theme.passport.entity.theme.UserBuyEntity;
import com.theme.passport.form.ApplePayElementByAdForm;
import com.theme.passport.form.ApplePayElementForm;
import com.theme.passport.form.ApplePayVipForm;
import com.theme.passport.service.theme.ApplePayService;
import com.theme.passport.service.theme.UserBuyService;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import java.util.HashMap;
import java.util.List;
import java.util.Map;

/**
 * @author: 可乐加糖
 * @create: 2023-02-21 16:00
 * @Description: 苹果支付回调: https://blog.csdn.net/m0_62949970/article/details/124570436
 */
@RestController
@RequestMapping("/app/apple")
@Api("Apple支付回调接口")
public class ApplePayController {

    private static Logger logger = LoggerFactory.getLogger(ApplePayController.class);

    @Autowired
    private ApplePayService applePayService;

    @Autowired
    private UserBuyService userBuyService;


    @Login
    @PostMapping("/appleBuyVip")
    @ApiOperation("Apple支付回调接口-购买会员")
    public R appleBuyVip(@RequestBody ApplePayVipForm applePayForm) {
        //表单校验
        ValidatorUtils.validateEntity(applePayForm);

        logger.info("Apple支付回调接口-购买会员 收到支付回调的接口参数：{}", JSONHandler.getJsonStr(applePayForm));
        UserEntity currentUser = AppContextUtil.getCurrentUser();
        applePayForm.setUserId(currentUser.getId());

        R r = applePayService.appleBuyVip(applePayForm);
        return r;

    }

    @Login
    @PostMapping("/appleBuyElement")
    @ApiOperation("Apple支付回调接口-购买主题组件壁纸接口")
    public R appleBuyElement(@RequestBody ApplePayElementForm applePayForm){

        //表单校验
        ValidatorUtils.validateEntity(applePayForm);

        logger.info("Apple支付回调接口-购买会员 收到支付回调的接口参数：{}", JSONHandler.getJsonStr(applePayForm));
        UserEntity currentUser = AppContextUtil.getCurrentUser();
        applePayForm.setUserId(currentUser.getId());

        Map<String, Object> params = new HashMap<>();
        params.put("userId",currentUser.getId() +"");
        params.put("type", String.valueOf(applePayForm.getType()));
        params.put("objectId",String.valueOf(applePayForm.getObjectId()));
        List<UserBuyEntity> userBuyList = userBuyService.queryList(params);
        if (userBuyList != null && userBuyList.size() > 0){
            return R.error(ErrorCode.USER_ALREADY_BUY_ELEMENT_EXIST.getCode(),ErrorCode.USER_ALREADY_BUY_ELEMENT_EXIST.getMsg());
        }

        R r = applePayService.appleBuyElement(applePayForm);
        return r;
    }

    @Login
    @PostMapping("/appleBuyElementByAd")
    @ApiOperation("广告下载记录")
    public R appleBuyElementByAd(@RequestBody ApplePayElementByAdForm applePayForm){

        //表单校验
        ValidatorUtils.validateEntity(applePayForm);

        logger.info("广告下载记录 收到支付回调的接口参数：{}", JSONHandler.getJsonStr(applePayForm));
        UserEntity currentUser = AppContextUtil.getCurrentUser();
        applePayForm.setUserId(currentUser.getId());
        applePayForm.setPrice(0L);
        Map<String, Object> params = new HashMap<>();
        params.put("userId",currentUser.getId() +"");
        params.put("type", String.valueOf(applePayForm.getType()));
        params.put("objectId",String.valueOf(applePayForm.getObjectId()));
        List<UserBuyEntity> userBuyList = userBuyService.queryList(params);
        if (userBuyList != null && userBuyList.size() > 0){
            return R.error(ErrorCode.USER_ALREADY_BUY_ELEMENT_EXIST.getCode(),ErrorCode.USER_ALREADY_BUY_ELEMENT_EXIST.getMsg());
        }

        R r = applePayService.appleBuyElementByAd(applePayForm);
        return r;
    }

}
